/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package it.allerj.gui.support;

import it.allerj.analysis.BlobDummyFilter;
import it.allerj.analysis.BlobFoodFilter;
import it.allerj.analysis.DotMark;
import it.allerj.analysis.FilmAnalyzer;
import it.allerj.demo.DemoConfiguratorMatrix;
import it.allerj.gui.action.FactoryGuiActionProxy;
import it.allerj.model.AllergicElement;
import it.allerj.model.AllergicTest;
import it.allerj.model.CellMatrix;
import it.allerj.model.Frame;
import it.allerj.model.PersistedMatrix;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Date;

/**
 *
 * @author Alessandro Veracchi
 */
public class AllergicTestFactory {

    public static AllergicTest getAllergicTest(BufferedImage image, String matrixName, String barcodeFrame, AllergicTest allergicTest){
         // instance a FilmAnalyzer and INIT! pass a dummy filter, it's enough...
        FilmAnalyzer analyzer = new FilmAnalyzer(image, 9, 9);
        
        // configure analyzer
        analyzer.refPointThreshold = 0.3f;
        analyzer.foodPointThresholdStart = 0.8f;
        analyzer.foodPointThresholdEnd = 0.96f;
        analyzer.foodPointSamplingSteps = 15;
        analyzer.circularityTolerance = 0.3f;
        
        // init analysis system
        if (!analyzer.init( new BlobDummyFilter() )) {
            //System.out.println("Unable to find black reference points!");
            return null;
        }
         // retrieve reference marks to paint them
        //ArrayList<DotMark> refs = analyzer.getRefDotMarks();         
        //paintRefWithCircles(image, refs);

        // configure a suitable filter to discard bad blobs
        // instance a Blob filtering object to discard bad blobs
        BlobFoodFilter filter = new BlobFoodFilter();
        
        // set blob filter global configuration
        filter.whRatioTolerance = 0.3f;
        filter.minAreaTolerancePerc = 30;
        filter.maxAreaTolerancePerc = 90;
        if(allergicTest==null){
            allergicTest = new AllergicTest();
           
        }
        allergicTest.setDate(new Date());
        if(allergicTest.getFrameList()==null){
            allergicTest.setFrameList(new ArrayList<Frame>());
        }
        Frame frame = createEmptyFrameByMatrix(barcodeFrame, matrixName);
        PersistedMatrix matrix = frame.getMatrix();
        
       
        // for every cell, retrieve mark if one was found        
        
        for (int i = 1; i < 2; i++) {
            for (int j = 0; j < 9; j++) {
                System.out.println("\ncella ("+i+","+j+")");
                
                // retrieve image area where to find dot mark
                BufferedImage cellImage = analyzer.getSamplingCell(i, j).image;
                
                // refine filter configuration
                filter.imageWidth = cellImage.getWidth();
                filter.imageHeight = cellImage.getHeight();
                
                // retrieve dotmark and store it (could be an empty DotMark)
                DotMark dotMark = analyzer.getFoodDotMark(cellImage, filter);
                CellMatrix cellMatrix=null;
                if(dotMark.getDiameter()!=0){
                    cellMatrix = matrix.getElement(i, j);
                    
                    AllergicElement allergicElement = new AllergicElement();
                    allergicElement.setType(cellMatrix.getValueCell());
                    allergicElement.setValue(1);
                    frame.getAllergicElementList().add(allergicElement);
                    
                }
            }
        }
        
        allergicTest.getFrameList().add(frame);
        return allergicTest;
    }
    public static Frame createEmptyFrameByMatrix(String barCode, String matrixName){
        Frame frame = new Frame();
        frame.setBarcode(barCode);
        if(frame.getAllergicElementList()==null){
            frame.setAllergicElementList(new ArrayList<AllergicElement>());
        }
       
         DemoConfiguratorMatrix dcm = new DemoConfiguratorMatrix();
        dcm.setGuiAction(FactoryGuiActionProxy.standardGuiAction);
        frame.setMatrix(dcm.getMatrix(matrixName));
         
        
        return frame;
    }
}
